package com.library.common.widget

import android.content.Context
import android.content.res.ColorStateList
import android.graphics.drawable.GradientDrawable
import android.graphics.drawable.StateListDrawable
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatTextView
import com.library.common.R

class BGButton(context: Context, attrs: AttributeSet) :
    AppCompatTextView(context, attrs) {
    private val gdp = GradientDrawable()
    private val gdn = GradientDrawable()
    private var topLeft = 0f
    private var bottomLeft = 0f
    private var topRight = 0f
    private var bottomRight = 0f
    private var pressedTextColor = 0
    private var normalTextColor = 0
    private var normalSolid = 0
    private var pressedSolid = 0
    private fun initAttr(
        context: Context,
        attrs: AttributeSet
    ) {
        val array = context.obtainStyledAttributes(attrs, R.styleable.BGButton)
        val radii = array.getDimension(R.styleable.BGButton_radii, -1f)
        if (radii > -1) setRadii(radii)
        val topLeft = array.getDimension(R.styleable.BGButton_topLeftRadius, -1f)
        if (topLeft != -1f) setTopLeftRadius(topLeft)
        val topRight = array.getDimension(R.styleable.BGButton_topRightRadius, -1f)
        if (topRight != -1f) setTopRightRadius(topRight)
        val bottomLeft =
            array.getDimension(R.styleable.BGButton_bottomLeftRadius, -1f)
        if (bottomLeft != -1f) setBottomLeftRadius(bottomLeft)
        val bottomRight =
            array.getDimension(R.styleable.BGButton_bottomRightRadius, -1f)
        if (bottomRight != -1f) setBottomRightRadius(bottomRight)
        normalSolid = array.getColor(R.styleable.BGButton_normalSolid, 0)
        setNormalSolid(normalSolid)
        pressedSolid = array.getColor(R.styleable.BGButton_pressedSolid, 0)
        setPressedSolid(pressedSolid)
        val normalStrokeWidth =
            (array.getDimension(R.styleable.BGButton_normalStrokeWidth, 0f) + .5f).toInt()
        val normalColor = array.getColor(R.styleable.BGButton_normalStrokeColor, 0)
        setNormalStroke(normalStrokeWidth, normalColor)
        val pressedStrokeWidth = (array.getDimension(
            R.styleable.BGButton_pressedStrokeWidth,
            normalStrokeWidth.toFloat()
        ) + .5f).toInt()
        val pressedColor = array.getColor(
            R.styleable.BGButton_pressedStrokeColor,
            context.resources.getColor(R.color.transparent)
        )
        setPressedStroke(pressedStrokeWidth, pressedColor)
        normalTextColor =
            array.getColor(R.styleable.BGButton_normalTextColor, currentTextColor)
        pressedTextColor =
            array.getColor(R.styleable.BGButton_pressedTextColor, normalTextColor)
        setTextSateListColor()
        array.recycle()
    }

    fun setBtnEnable(enable: Boolean) {
        isEnabled = enable
    }

    fun setTopLeftRadius(topLeftRadius: Float) {
        topLeft = topLeftRadius
        resetRadius()
    }

    fun setTopRightRadius(topRightRadius: Float) {
        topRight = topRightRadius
        resetRadius()
    }

    fun setBottomLeftRadius(bottomLeftRadius: Float) {
        bottomLeft = bottomLeftRadius
        resetRadius()
    }

    fun setBottomRightRadius(bottomRightRadius: Float) {
        bottomRight = bottomRightRadius
        resetRadius()
    }

    fun setRadii(radii: Float) {
        bottomLeft = radii
        bottomRight = bottomLeft
        topRight = bottomRight
        topLeft = topRight
        resetRadius()
    }

    fun setNormalSolid(color: Int) {
        gdn.setColor(color)
        if (pressedSolid == 0) {
            gdp.setColor(color)
        }
    }

    fun setPressedSolid(color: Int) {
        gdp.setColor(color)
    }

    fun setNormalStroke(width: Int, color: Int) {
        gdn.setStroke(width, color)
    }

    fun setPressedStroke(width: Int, color: Int) {
        gdp.setStroke(width, color)
    }

    fun setPressedTextColor(pressedTextColor: Int) {
        this.pressedTextColor = pressedTextColor
        setTextSateListColor()
    }

    override fun setTextColor(color: Int) {
        normalTextColor = color
        pressedTextColor = color
        setTextSateListColor()
    }

    private fun resetRadius() {
        val radii = floatArrayOf(
            topLeft,
            topLeft,
            topRight,
            topRight,
            bottomRight,
            bottomRight,
            bottomLeft,
            bottomLeft
        )
        gdn.cornerRadii = radii
        gdp.cornerRadii = radii
    }

    private fun setTextSateListColor() {
        val state = arrayOfNulls<IntArray>(2)
        state[0] = intArrayOf(android.R.attr.state_pressed)
        state[1] = intArrayOf()
        val colors = intArrayOf(pressedTextColor, normalTextColor)
        val colorStateList = ColorStateList(state, colors)
        setTextColor(colorStateList)
    }

    init {
        val bg = StateListDrawable()
        bg.addState(intArrayOf(android.R.attr.state_pressed), gdp)
        bg.addState(intArrayOf(), gdn)
        setBackgroundDrawable(bg)
        initAttr(context, attrs)
    }
}